package com.diorsunion.hedge.client.borker;

import com.diorsunion.hedge.bo.db.AccountBO;
import com.diorsunion.hedge.dal.entity.account.Account;
import com.diorsunion.hedge.dal.entity.account.AccountAssets;
import com.diorsunion.hedge.dal.repository.account.AccountRepository;
import com.diorsunion.hedge.dto.AccountSummary;
import com.diorsunion.hedge.dto.BorkerAccount;
import com.diorsunion.hedge.enums.BorkerType;
import com.diorsunion.hedge.io.IOClient;
import com.diorsunion.hedge.util.LogUtil;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by wangnima on 2016/5/29.
 */
@Component
public class ClientBorkerBO {
    @Resource
    IOClient ioClient;

    @Resource
    AccountBO accountBO;

    Map<Integer,ClientBorkerCmd> cmdMap  = Maps.newConcurrentMap();
    Map<String, AccountSummary> accountSummaryCacheMap = Maps.newConcurrentMap();

    public void addCmd(ClientBorkerCmd clientBorkerCmd){
        cmdMap.put(clientBorkerCmd.id, clientBorkerCmd);
    }

    @Resource
    AccountRepository accountRepository;
    /**
     * 登录
     * @param borkerCode 登陆账号ID
     */
    public void onConnect(String borkerCode, BorkerType borkerType){
        BorkerAccount borkerAccount = new BorkerAccount();
        borkerAccount.code = borkerCode;
        borkerAccount.borkerType = borkerType;
        accountBO.addNewAccount(borkerAccount);
        //发送一个帐号登录的指令
//        Message<BorkerAccount> message = Message.genDateMessage("login",borkerAccount);
        /**
         * 还是直接写数据库吧，简单一点
         */
//        ioClient.write(message);

    }

    public synchronized void sendAccountSummary() {
        accountSummaryCacheMap.forEach((accountCode, accountSummary) -> {
//            Message<AccountSummary> message = Message.genDateMessage("accountSummary", accountSummary);
//            ioClient.write(message);
            Account account = new Account();
            account.borkerType = accountSummary.borkerType;
            account.code = accountSummary.accountCode;
            AccountAssets accountAssets = new AccountAssets();
            Map<String, String> m = accountSummary.tagValueMap;
            accountAssets.accruedCash = m.containsKey("AccruedCash") ? Double.parseDouble(m.get("AccruedCash")) : null;
            accountAssets.availableFunds = m.containsKey("AvailableFunds") ? Double.parseDouble(m.get("AvailableFunds")) : null;
            accountAssets.buyingPower = m.containsKey("BuyingPower") ? Double.parseDouble(m.get("BuyingPower")) : null;
            accountAssets.equityWithLoanValue = m.containsKey("EquityWithLoanValue") ? Double.parseDouble(m.get("EquityWithLoanValue")) : null;
            accountAssets.excessLiquidity = m.containsKey("ExcessLiquidity") ? Double.parseDouble(m.get("ExcessLiquidity")) : null;
            accountAssets.fullAvailableFunds = m.containsKey("FullAvailableFunds") ? Double.parseDouble(m.get("FullAvailableFunds")) : null;
            accountAssets.fullExcessLiquidity = m.containsKey("FullExcessLiquidity") ? Double.parseDouble(m.get("FullExcessLiquidity")) : null;
            accountAssets.fullInitMarginReq = m.containsKey("FullInitMarginReq") ? Double.parseDouble(m.get("FullInitMarginReq")) : null;
            accountAssets.fullMaintMarginReq = m.containsKey("FullMaintMarginReq") ? Double.parseDouble(m.get("FullMaintMarginReq")) : null;
            accountAssets.grossPositionValue = m.containsKey("GrossPositionValue") ? Double.parseDouble(m.get("GrossPositionValue")) : null;
            accountAssets.initMarginReq = m.containsKey("InitMarginReq") ? Double.parseDouble(m.get("InitMarginReq")) : null;
            accountAssets.lookAheadAvailableFunds = m.containsKey("LookAheadAvailableFunds") ? Double.parseDouble(m.get("LookAheadAvailableFunds")) : null;
            accountAssets.lookAheadExcessLiquidity = m.containsKey("LookAheadExcessLiquidity") ? Double.parseDouble(m.get("LookAheadExcessLiquidity")) : null;
            accountAssets.lookAheadInitMarginReq = m.containsKey("LookAheadInitMarginReq") ? Double.parseDouble(m.get("LookAheadInitMarginReq")) : null;
            accountAssets.lookAheadMaintMarginReq = m.containsKey("LookAheadMaintMarginReq") ? Double.parseDouble(m.get("LookAheadMaintMarginReq")) : null;
            accountAssets.maintMarginReq = m.containsKey("MaintMarginReq") ? Double.parseDouble(m.get("MaintMarginReq")) : null;
            accountAssets.netLiquidation = m.containsKey("NetLiquidation") ? Double.parseDouble(m.get("NetLiquidation")) : null;
            accountAssets.regtEquity = m.containsKey("RegTEquity") ? Double.parseDouble(m.get("RegTEquity")) : null;
            accountAssets.regtMargin = m.containsKey("RegTMargin") ? Double.parseDouble(m.get("RegTMargin")) : null;
            accountAssets.sma = m.containsKey("SMA") ? Double.parseDouble(m.get("SMA")) : null;
            accountAssets.totalCashValue = m.containsKey("TotalCashValue") ? Double.parseDouble(m.get("TotalCashValue")) : null;
            accountAssets.settledCash = m.containsKey("SettledCash") ? Double.parseDouble(m.get("SettledCash")) : null;
            accountBO.insertAccountAssets(account, accountAssets);
            LogUtil.info("accountSummary:"+accountSummary);
        });
        accountSummaryCacheMap.clear();
    }


    public synchronized void putAccountSummary(String accountCode, BorkerType borkerType, String tag, String value, String currency) {
        AccountSummary accountSummary = null;
        if (accountSummaryCacheMap.containsKey(accountCode)) {
            accountSummary = accountSummaryCacheMap.get(accountCode);
        } else {
            accountSummary = new AccountSummary();
            accountSummary.accountCode = accountCode;
            accountSummary.borkerType = borkerType;
            accountSummaryCacheMap.put(accountCode, accountSummary);
        }
        accountSummary.put(tag, value);
    }
}
